package com.jiutong.meipenggang.ui.activitys;


import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.InvestDetailBean;
import com.jiutong.meipenggang.http.model.interfaces.InvestDetailBiz;
import com.jiutong.meipenggang.http.present.InvestDetailPresent;
import com.jiutong.meipenggang.utils.NumUilt;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.views.OnRulerChangeListener;
import com.jiutong.meipenggang.views.RulerView;
import com.jiutong.meipenggang.views.dialog.BaseDialog;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.Bind;
import butterknife.OnClick;

public class InvestDetailActivity extends BaseActivity implements InvestDetailBiz {

    @Bind(R.id.rl_common_title_back)
    RelativeLayout rlCommonTitleBack;
    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.tv_common_title_options)
    TextView tvCommonTitleOptions;
    @Bind(R.id.tv_item_invest_title)
    TextView tvItemInvestTitle;
    @Bind(R.id.tv_item_invest_status)
    TextView tvItemInvestStatus;
    @Bind(R.id.tv_item_invest_rate)
    TextView tvItemInvestRate;
    @Bind(R.id.tv_item_invest_money)
    TextView tvItemInvestMoney;
    @Bind(R.id.ll_right)
    LinearLayout llRight;
    @Bind(R.id.tv_item_invest_time)
    TextView tvItemInvestTime;
    @Bind(R.id.item_progressbar)
    ProgressBar itemProgressbar;
    @Bind(R.id.tv_item_progress)
    TextView tvItemProgress;
    @Bind(R.id.tv_item_invest_time_text)
    TextView tvItemInvestTimeText;
    @Bind(R.id.tv_invest_detail_project_describe)
    TextView tvInvestDetailProjectDescribe;
    @Bind(R.id.tv_invest_detail_year_income)
    TextView tvInvestDetailYearIncome;
    @Bind(R.id.tv_invest_detail_financing_limit)
    TextView tvInvestDetailFinancingLimit;
    @Bind(R.id.tv_invest_detail_financing)
    TextView tvInvestDetailFinancing;
    @Bind(R.id.tv_invest_detail_start_time)
    TextView tvInvestDetailStartTime;
    @Bind(R.id.tv_invest_detail_repayment_way)
    TextView tvInvestDetailRepaymentWay;
    @Bind(R.id.tv_invest_detail_interest_time)
    TextView tvInvestDetailInterestTime;
    @Bind(R.id.tv_invest_detail_project_status)
    TextView tvInvestDetailProjectStatus;
    @Bind(R.id.ll_invest_detail_risk)
    LinearLayout llInvestDetailRisk;
    @Bind(R.id.ll_invest_detail_security)
    LinearLayout llInvestDetailSecurity;
    @Bind(R.id.ll_invest_detail_repayment_plan)
    LinearLayout llInvestDetailRepaymentPlan;
    @Bind(R.id.ll_invest_detail_record)
    LinearLayout llInvestDetailRecord;
    @Bind(R.id.tv_invest_detail_ruler_money)
    TextView tvInvestDetailRulerMoney;
    @Bind(R.id.tv_invest_detail_earn_money)
    TextView tvInvestDetailEarnMoney;
    @Bind(R.id.tv_invest_detail_rv_value)
    TextView tvInvestDetailRvValue;
    @Bind(R.id.invest_dtail_rulerView)
    RulerView investDtailRulerView;
    @Bind(R.id.sv_container)
    ScrollView svContainer;
    @Bind(R.id.btn_invest_detail_competitive)
    Button btnInvestDetailCompetitive;
    private int bidId;

    private InvestDetailPresent investDetailPresent;
    private BaseDialog dialog;

    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_invest_detail;

    }

    @Override
    protected void initView() {
        super.initView();
        tvCommonTitle.setText("投资详情");

        Bundle extras = getIntent().getExtras();
        bidId = extras.getInt("bidId");
        if (bidId < 0) {
            return;
        }

        investDetailPresent = new InvestDetailPresent(this, this);
        investDetailPresent.getInvestDetailInfo();


        //尺子监听回调
        investDtailRulerView.setOnRulerChangeListener(new OnRulerChangeListener() {
            @Override
            public void onChanged(int newValue) {
                float persent = (float) newValue / 200000;
                tvInvestDetailRulerMoney.setText(newValue + "");
                tvInvestDetailEarnMoney.setText(newValue * 0.05 + "");
                tvInvestDetailRvValue.setText(newValue + "");
            }
        });

        //冲突解决
        investDtailRulerView.setOnTouchListener(new View.OnTouchListener() {

            private float mLastX;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                if (action == MotionEvent.ACTION_DOWN) {
                    mLastX = event.getX();
                }
                if (action == MotionEvent.ACTION_MOVE) {

                    if (Math.abs(event.getX() - mLastX) > 30f) {
                        svContainer.requestDisallowInterceptTouchEvent(true);
                    }
                }
                if (action == MotionEvent.ACTION_UP) {
                    svContainer.requestDisallowInterceptTouchEvent(false);
                }
                if (action == MotionEvent.ACTION_CANCEL) {
                    svContainer.requestDisallowInterceptTouchEvent(false);
                }

                return false;
            }
        });


    }

    @OnClick({R.id.rl_common_title_back, R.id.ll_invest_detail_risk, R.id.ll_invest_detail_security, R.id.ll_invest_detail_repayment_plan, R.id.ll_invest_detail_record, R.id.btn_invest_detail_competitive})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                AppManager.getAppManager().finishActivity(this);
                break;
            case R.id.ll_invest_detail_risk:  //风控信息
                openActivity(InvestRiskActivity.class);
                break;
            case R.id.ll_invest_detail_security:  //安全保障
                openActivity(InvestSecurityActivity.class);
                break;
            case R.id.ll_invest_detail_repayment_plan:  //还款计划
                Bundle bundle = new Bundle();
                bundle.putInt("bidId", bidId);
                openActivity(InvestRepaymentAcitivty.class, bundle);
                break;
            case R.id.ll_invest_detail_record:  //投资记录
                Bundle bundle1 = new Bundle();
                bundle1.putInt("bidId", bidId);
                openActivity(InvestRecordActivity.class, bundle1);
                break;
            case R.id.btn_invest_detail_competitive:   //马上竞标
                if (!SpConfig.getInstance().getBool(Constants.IS_LOGIN)) {
                    showLoginDialog();
                }
//                else if(!SpConfig.getInstance().getBool(Constants.BIND_CARD_FLAG)){
////                    showBindBankCradDialog();
//                }
                else {
                    Bundle bundle2 = new Bundle();
                    bundle2.putInt("bidId", bidId);
                    openActivity(InvestMoneyActivity.class, bundle2);
                }
                break;
            default:
        }
    }

    /**
     * 还未绑定银行卡，显示绑定银行卡dialog
     */
    private void showBindBankCradDialog() {
        if (dialog == null) {
            dialog = new BaseDialog.Builder().
                    layResuorce(R.layout.dialog_common).
                    cancelViewClickEvent(R.id.tv_dialog_cancel).
                    okViewClickEvent(R.id.tv_dialog_ok).
                    ok2ViewClickEvent(R.id.tv_dialog_ok).
                    confirmId(R.id.tv_dialog_confirm).
                    llBottomId(R.id.ll_dialog).
                    contentTextId(R.id.tv_dialog_content).
                    hintTextId(R.id.tv_dialog_hint).
                    setHintText("温馨提示").
                    setContentText("您还未绑卡，请先去绑卡充值").
                    setConfirmText("确定").
                    showOneOrTwoBtn(false).
                    gravityStyle(Gravity.CENTER).
                    animResource(R.style.dialog_animation_fade).
                    dialogWidth(-2).
                    build();
            dialog.setOnDialogClickListener(new BaseDialog.OnDialogClickListener() {
                @Override
                public void onOkClick() {
                    dialog.dismiss();
                    openActivity(BindingBankCardActivity.class);
                }

                @Override
                public void onOk2Click() {
                    dialog.dismiss();
                }
            });
        }

        dialog.show(getSupportFragmentManager(), TAG);
    }

    /**
     * 还未登录，显示去登录dialog
     */
    public void showLoginDialog() {
        if (dialog == null) {
            dialog = new BaseDialog.Builder().
                    layResuorce(R.layout.dialog_common).
                    cancelViewClickEvent(R.id.tv_dialog_cancel).
                    okViewClickEvent(R.id.tv_dialog_ok).
                    confirmId(R.id.tv_dialog_confirm).
                    llBottomId(R.id.ll_dialog).
                    contentTextId(R.id.tv_dialog_content).
                    hintTextId(R.id.tv_dialog_hint).
                    setHintText("温馨提示").
                    setContentText("亲，您还没有登录，去登录？").
                    setCancelText("否").
                    setOkText("是").
                    gravityStyle(Gravity.CENTER).
                    animResource(R.style.dialog_animation_fade).
                    dialogWidth(-2).
                    build();
            dialog.setOnDialogClickListener(new BaseDialog.OnDialogClickListener() {
                @Override
                public void onOkClick() {
                    if(dialog!=null){
                        dialog.dismiss();
                    }
                    isLogin();
                }

                @Override
                public void onOk2Click() {

                }
            });
        }
        dialog.show(getSupportFragmentManager(), TAG);
    }

    @Override
    public String getBidId() {
        return bidId + "";
    }

    @Override
    public void onSuccess(Object object) {
        InvestDetailBean bean = (InvestDetailBean) object;
        if (bean.getError() == -1) {
            InvestDetailBean.BidBean bidBean = bean.getBid();
            //项目名称
            tvItemInvestTitle.setText(bidBean.getTitle());
            tvInvestDetailProjectDescribe.setText(bidBean.getTitle());
            //年华收益
            tvInvestDetailYearIncome.setText(bidBean.getApr() + "%");
            tvItemInvestRate.setText(bidBean.getApr() + "");
            //融资期限
            tvInvestDetailFinancingLimit.setText(bidBean.getPeriod() + "天");
            //融资金额
            tvInvestDetailFinancing.setText(NumUilt.doubleWanFormat(bidBean.getAmount() + "") + "元");
            //开始时间
            SimpleDateFormat sdf1 = new SimpleDateFormat("yy年MM月dd日");
            Date date1 = new Date();
            date1.setTime(Long.parseLong(bidBean.getBidding_time()));
            tvInvestDetailStartTime.setText(sdf1.format(date1));
            //还款方式
            tvInvestDetailRepaymentWay.setText(bidBean.getRepayName());
            //起息时间
            tvInvestDetailInterestTime.setText(bidBean.getInterestTime() + "");
            //投资期限
            tvItemInvestTime.setText(bidBean.getPeriod() + "天");
            //剩余金额
            tvItemInvestTimeText.setText("剩余金额");
            double overplusMoney = bidBean.getAmount() - bidBean.getHasInvestedAmount();
            tvItemInvestMoney.setText(NumUilt.doubleWanFormat(overplusMoney + ""));
            switch (bidBean.getStatus()) {
                case 1:
                case 2:
                    long time = Long.parseLong(bidBean.getBidding_time());
                    long currentTime = System.currentTimeMillis();
                    if (time <= currentTime) {
                        tvItemInvestStatus.setText("投标中");
                        tvItemInvestStatus.setBackgroundResource(R.mipmap.invest_item_blue);
                        //项目状态
                        tvInvestDetailProjectStatus.setText("投标中");
                    } else {
                        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                        Date date = new Date();
                        date.setTime(time);
                        tvItemInvestStatus.setText(sdf.format(date) + "开始");
                        tvInvestDetailProjectStatus.setText("未开始");
                    }
                    break;
                case 3:
                    tvItemInvestStatus.setText("满标");
                    tvItemInvestStatus.setBackgroundResource(R.mipmap.invest_item_gray);
                    tvInvestDetailProjectStatus.setText("满标");
                    break;
                case 4:
                case 14:
                    tvItemInvestStatus.setText("还款中");
                    tvItemInvestStatus.setBackgroundResource(R.mipmap.invest_item_gray);
                    tvInvestDetailProjectStatus.setText("还款中");
                    break;
                case 5:
                    tvItemInvestStatus.setText("投资结束");
                    tvItemInvestStatus.setBackgroundResource(R.mipmap.invest_item_gray);
                    tvInvestDetailProjectStatus.setText("投资结束");
                    break;
            }
        } else {
            toastShortShow(bean.getMsg());
        }
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 52;
    }


}
